using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace MapEditor
{
    public partial class PreviewFrm : Form
    {
        public PreviewFrm()
        {
            InitializeComponent();
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);

            Graphics g = Graphics.FromImage(SaveFrm.saveBitmap);
            e.Graphics.DrawImage(Program.backGround, this.ClientRectangle, new Rectangle(0, 0, Program.backGround.Width, Program.backGround.Height), GraphicsUnit.Pixel);
            g.DrawImage(Program.backGround, this.ClientRectangle, new Rectangle(0, 0, Program.backGround.Width, Program.backGround.Height), GraphicsUnit.Pixel);

            int next = 1;
            while (next < arr.Length)
            {
                int x = arr[next - 1].X;
                int y = arr[next - 1].Y;
                if (x == this.ClientRectangle.Width)
                    x -= width;
                if (y == this.ClientRectangle.Height)
                    y -= height;
                int fx = 1, fy = 1;
                if (arr[next].X - arr[next - 1].X == 0)
                {
                    fx = 0;
                }
                else if (arr[next].X - arr[next - 1].X < 0)
                {
                    fx = -1;
                }
                else
                {
                    fx = 1;
                }

                if (arr[next].Y - arr[next - 1].Y == 0)
                {
                    fy = 0;
                }
                else if (arr[next].Y - arr[next - 1].Y < 0)
                {
                    fy = -1;
                }
                else
                {
                    fy = 1;
                }
                while ((x - arr[next].X) * fx <= 0 && (y - arr[next].Y) * fy <= 0)
                {
                    e.Graphics.DrawImage(Program.path, new Rectangle(x, y, width, height), new Rectangle(0, 0, Program.path.Width, Program.path.Height), GraphicsUnit.Pixel);
                    g.DrawImage(Program.path, new Rectangle(x, y, width, height), new Rectangle(0, 0, Program.path.Width, Program.path.Height), GraphicsUnit.Pixel);
                    x += width * fx;
                    y += height * fy;
                }
                next++;
            }
            g.Dispose();
        }

        private void PreviewFrm_Load(object sender, EventArgs e)
        {
            LoadXML();
        }

        Point[] arr;
        int width, height;
        private void LoadXML()
        {
            width = this.ClientRectangle.Width / 25;
            width *= 3;
            height = this.ClientRectangle.Height / 25;
            height *= 3;

            // doc file xml
            XmlTextReader reader = new XmlTextReader(SaveFrm.startup + "\\data\\map.xml");

            reader.ReadToFollowing("map");
            int nums = int.Parse(reader.GetAttribute("point"));

            arr = new Point[nums];
            for (int i = 0; i < nums; i++)
            {
                reader.ReadToFollowing("point");
                reader.ReadToFollowing("x");
                int x = int.Parse(reader.ReadString());
                reader.ReadToFollowing("y");
                int y = int.Parse(reader.ReadString());

                // convert to full screen
                x = x * this.ClientRectangle.Width / 100;
                y = y * this.ClientRectangle.Height / 100;

                arr[i] = new Point(x, y);
            }

            reader.Close();


            SaveFrm.saveBitmap = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
        }

        private void PreviewFrm_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // Rocker Up
                // Up
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // Rocker Down
                // Down
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // Left
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // Right
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                // Enter
                Close();
            }

        }
    }
}